package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbAttaMapper;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.pojo.TbAtta;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImage;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.ProdPageVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-05-26 16:48:08
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    TbProdImageMapper tbProdImageMapper;
    @Autowired
    TbAttaMapper tbAttaMapper;
    @Override
    public Result getProdList(ProdPageVo prodPageVo) {
        //1.构造分页参数
        Page<TbProd> tbProdPage = new Page<>(prodPageVo.getPageNum(), prodPageVo.getPageSize());
       //2.分页查询
//        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
        Page<TbProd> prodPage = tbProdMapper.getProdList(tbProdPage, prodPageVo);
        return Result.ok().setData(prodPage);
    }

    @Override
    public Result addProd(TbProd tbProd) {
        String token = request.getHeader("token");
        if(token==null){
            return Result.error().setMsg("请选登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);
        //1.重复校验
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu()));
        if(prod!=null){
            return Result.error().setMsg("spu商品已存在");
        }
         prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName()));
        if(prod!=null){
            return Result.error().setMsg("商品名称已存在");
        }
        //2.添加商品
        tbProd.setCreateTime(new Date());
        tbProd.setCreateBy(nickName);
        tbProd.setProdStatus(0);
        tbProdMapper.insert(tbProd);
        return Result.ok();
    }

    @Override
    public Result deleteAllProd(List<TbProd> tbProdList) {

        for (TbProd tbProd : tbProdList) {
            tbProdMapper.deleteById(tbProd.getProdId());
        }

        return Result.ok();
    }

    @Override
    public Result addProdAtta(TbAtta tbAtta) {
        //1.确定商品存在
        TbProd tbProd = tbProdMapper.selectById(tbAtta.getProdId());
        if(tbProd==null){
            return Result.error().setMsg("商品不存在");
        }
        //2.更新商品信息
        tbProd.setProdVideo(tbAtta.getProdVideo());
        tbProd.setUpType(tbAtta.getUpType());
        tbProd.setUpTime(tbAtta.getUpTime());
        tbProd.setDownType(tbAtta.getDownType());
        tbProd.setDownTime(tbAtta.getDownTime());
        tbProdMapper.updateById(tbProd);

        //先删除
        tbProdImageMapper.delete(new QueryWrapper<TbProdImage>().lambda().eq(TbProdImage::getProdId,tbProd.getProdId()));
        //3.添加商品轮播图
        if(tbAtta.getImageList()!=null && tbAtta.getImageList().size()>0){
            for (TbProdImage tbProdImage : tbAtta.getImageList()) {
                tbProdImage.setProdId(tbProd.getProdId());
                tbProdImageMapper.insert(tbProdImage);
            }
        }
        tbAttaMapper.insert(tbAtta);
        return Result.ok();
    }
}




